package cn.fws.meap.community.service.impl;

import cn.fws.meap.community.mapper.AnswerMapper;
import cn.fws.meap.community.service.DetailsUtils;
import cn.fws.meap.community.vo.Details;
import cn.fws.meap.user.mapper.BrowseMapper;
import cn.fws.meap.user.mapper.CollectMapper;
import cn.fws.meap.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DetailsUtilsImpl<T> implements DetailsUtils<T> {
    @Autowired
    private BrowseMapper browseMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CollectMapper collectMapper;
    @Autowired
    private AnswerMapper answerMapper;

    /**
     * 根据泛型,文章ID和openID返回该类型的详情信息
     * 返回details之后需要setT
     * @param <T>
     */
    @Override
    public Details<T> getDetails(String artId, String openId,String type) {
        //  获取文章访问量
        int AccessCount = browseMapper.getCountByArticleId(artId);
        //根据openId 获取用户昵称
        String nickName = userMapper.getNickNameByOpenId(openId);
        //根据openId获取用户头像
        String userPicture = userMapper.getUserPictureByOpenId(openId);
        //根据文章(问题,资讯)ID获取收藏数量
        int collectCount = collectMapper.getCollectCountByArticleId(artId);
        //根据文章ID获取回答数量
        int answerCount = answerMapper.getCountByProblemId(artId);
        //创建实体类对象
        Details<T> details = new Details<>();
        //添加访问量
        details.setAccessCount(AccessCount);
        //添加用户昵称
        details.setUserName(nickName);
        //添加用户头像
        details.setUserPicture(userPicture);
        //添加收藏量
        details.setCollent(collectCount);
        //添加回答数量
        details.setAnswerCount(answerCount);
        //设置详情类型
        details.setType(type);

        return details;
    }
}

